/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.user.impl.service;

import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.jiantong.user.ext.UserDepartment;
import com.jiantong.user.impl.dao.DepartmentUserDao;
import com.jiantong.user.model.DepartmentUser;
import com.jiantong.user.service.DepartmentUserReadService;
import com.sun.org.apache.regexp.internal.RE;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : panxin
 */
@Slf4j
@Service
@RpcProvider
public class DepartmentUserReadServiceImpl implements DepartmentUserReadService {

    private final DepartmentUserDao departmentUserDao;

    @Autowired
    public DepartmentUserReadServiceImpl(DepartmentUserDao departmentUserDao) {
        this.departmentUserDao = departmentUserDao;
    }

    @Override
    public Response<DepartmentUser> findDepartmentUserByUserId(Long userId) {
        try {
            DepartmentUser user = departmentUserDao.findByUserId(userId);
            if (user == null) {
                log.error("department user is not exists, id = {}", userId);
                return Response.fail("user.not.exists");
            }
            return Response.ok(user);
        } catch (Exception e) {
            log.error("failed to find department user by userId = {}, cause : {}",
                    userId, Throwables.getStackTraceAsString(e));
            return Response.fail("find.department.user.failed");
        }
    }

    @Override
    public Response<Optional<DepartmentUser>> findOptionalDepartmentUserByUserId(Long userId) {
        try {
            return Response.ok(Optional.fromNullable(departmentUserDao.findByUserId(userId)));
        } catch (Exception e) {
            log.error("failed to find department user by userId = {}, cause : {}",
                    userId, Throwables.getStackTraceAsString(e));
            return Response.fail("find.department.user.failed");
        }
    }

    @Override
    public Response<List<UserDepartment>> findDepartmentUsersByUserId(Long userId) {
        try{
            List<DepartmentUser> departmentUserList = departmentUserDao.findDepartmentUsersByUserId(userId);
            if(departmentUserList == null || departmentUserList.size() == 0){
                log.error("department user is not exists, id = {}", userId);
                return Response.fail("user.not.exists");
            }
            List<UserDepartment> userDepartmentList = new ArrayList<>();
            for (DepartmentUser departmentUser: departmentUserList) {
                userDepartmentList.add(
                        new UserDepartment(
                                departmentUser.getDepartmentId(),
                                departmentUser.getDepartmentName(),
                                departmentUser.getDepartmentType(),
                                departmentUser.getEmpType())
                );
            }
            return Response.ok(userDepartmentList);
        }catch (Exception e){
            log.error("failed to find department user by userId = {}, cause : {}",
                    userId, Throwables.getStackTraceAsString(e));
            return Response.fail("find.department.user.failed");
        }
    }

    /**
     * 条件查询user_id
     * @param criteria
     * @return
     */
    public Response<List<Long>> selectUserIds(Map criteria){
        try {
          return Response.ok(departmentUserDao.selectUserIds(criteria));
        } catch (Exception e) {
            log.error("failed to find department user by userId = {}, cause : {}",
                    Throwables.getStackTraceAsString(e));
            return Response.fail("find.department.user.failed");
        }
    }

    /**
     * 条件查询
     * @param criteria
     * @return
     */
    public Response<List<DepartmentUser>> selectList(Map criteria){
        try {
            return Response.ok(departmentUserDao.selectList(criteria));
        } catch (Exception e) {
            log.error("failed to find department user by userId = {}, cause : {}",
                    Throwables.getStackTraceAsString(e));
            return Response.fail("find.department.user.failed");
        }
    }
}
